<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Validator;
use App\Http\Model\CategoryModel;
use Illuminate\Http\Request;

class CategoryController extends Controller
{
    public function selectCategory(Request $req)
    {
        $cm = new CategoryModel();

        $selectRes = $cm->selectCategoryData(PAGE_NUM);

        $res = $this->getData(SUCCESS_CODE, $selectRes);

        return response()->json($res);
    }

    public function addCategory(Request $req)
    {
        $data = $this->getReqArray($req->getContent());

        $validator = Validator::make($data, [
            'name' => 'required',
            'sort' => 'required|integer|between:0,99',
            'states' => 'required|integer|between:0,1',
            'property1' => 'required'
        ]);

        if ($validator->fails()) {

            $res = $this->getMsg(ERROR_CODE, $validator->errors()->first());

            return response()->json($res);
        }

        if(CategoryModel::where("name",$data["name"])->first()){

            return $this->getMsg(ERROR_CODE,"分类名已存在");

        }

        $property = $this->getProperty($data);

        $time = date("Y-m-d H:i:s");

        $cm = new CategoryModel();

        $addRes = $cm->addCategoryData($data['name'], $property, $data['sort'], $data['states'], $time);

        $res = $this->getRes($addRes, "add");

        return response()->json($res);
    }

    private function getProperty($data)
    {
        $property = array();

        $property["property1"] = $data["property1"];

        if (!(is_null($data["property2"]) or $data["property2"] == "")) {
            $property["property2"] = $data["property2"];
            if (!(is_null($data["property3"]) or $data["property3"] == "")) {
                $property["property3"] = $data["property3"];
            }
        }

        $property = json_encode($property, JSON_FORCE_OBJECT);

        return $property;
    }

    public function updateCategory(Request $req)
    {
        $data = $this->getReqArray($req->getContent());

        $validator = Validator::make($data, [
            'id' => 'required|integer',
            'name' => 'required',
            'sort' => 'required|integer|between:0,99',
            'states' => 'required|integer|between:0,1',
            'property1' => 'required'
        ]);

        if ($validator->fails()) {

            $res = $this->getMsg(ERROR_CODE, $validator->errors()->first());

            return response()->json($res);
        }

        $id = $data['id'];

        $property = $this->getProperty($data);

        $time = date("Y-m-d H:i:s");

        $cm = new CategoryModel();

        $updateres = $cm->updateCategoryData($id, $data['name'], $property, $data['sort'], $data['states'], $time);

        $res = $this->getMsg($updateres, "update");

        return response()->json($res);

    }

    public function delCategory(Request $req)
    {

        $data = $this->getReqArray($req->getContent());

        $validator = Validator::make($data, [
            'id' => 'required|integer'
        ]);

        if ($validator->fails()) {

            $res = $this->getMsg(ERROR_CODE, $validator->errors()->first());

            return response()->json($res);
        }

        $id = $data['id'];

        if(empty(Category::where("id",$id)->first())){
            return $this->getMsg(ERROR_CODE,"找不到分类");
        }

        $cm = new CategoryModel();

        $delRes = $cm->delCategoryData($id);

        $res = $this->getRes($delRes, "del");

        return response()->json($res);
    }

    public function showCategory($id)
    {

        preg_match('/^\d+$/', $id, $match);

        if (count($match) == 0) {

            $res = $this->getMsg(ERROR_CODE, "ID错误");

            return response()->json($res);
        }

        $cm = new CategoryModel();

        $showRes = $cm->getCategory($id);

        if (empty($showRes)) {

            $msg = $this->getMsg(ERROR_CODE,"没有分类数据");

            return response()->json($msg);
        }

        $res = $this->getData(SUCCESS_CODE,$showRes);

        return response()->json($res);
    }

    public function getAllCategory()
    {
        $cm = new CategoryModel();

        $getRes = $cm->getAllCategory();

        if (empty($getRes)){
            return $this->getMsg(ERROR_CODE,"查询类别失败");
        }
        return $this->getData(SUCCESS_CODE,$getRes);

    }
}
